//
//  SKIndoorMapView.m
//  IndoorMapDemo
//
//  Created by silverk on 14-8-3.
//  Copyright (c) 2014年 silverk. All rights reserved.
//

#import "SKIndoorMapView.h"
#import "SKIndoorMapTransfer.h"
#import "SpotDetailViewController.h"
#import "UIImageView+LK.h"
#import "AppDelegate.h"
#import "DBManager.h"
#import "DXAlertView.h"

#import "CommonService.h"

@implementation SKIndoorMapView
@synthesize mutableArrayHotButton;
@synthesize mutableArraySpot;
@synthesize mapImageViewDelegate;
@synthesize mScale;
@synthesize lastScale;
@synthesize mapRouteView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

-(id)initWithIndoorMapImageName:(ParkEntity*)parkEntity
                          Frame:(CGRect)frame
                           Array:(NSMutableArray *)array
{
    if (self=[super init]) {
        self.bounces = NO;
        self.delegate = self;
        self.backgroundColor = [UIColor whiteColor];
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.frame = frame;
        
        if (parkEntity.map_width == nil) {
            ALERT_ERR_MSG(@"地图高度数据，获取失败");
            _mapView = [[UIImageView alloc]initWithFrame:
                        CGRectMake(-5, 0, 500, 1000)];
        } else {
            _mapView = [[UIImageView alloc]initWithFrame:
                        CGRectMake(-5, 0, [parkEntity.map_width doubleValue], [parkEntity.map_height doubleValue])];//
            mapRouteView = [[UIImageView alloc]initWithFrame:
                        CGRectMake(-5, 0, [parkEntity.map_width doubleValue], [parkEntity.map_height doubleValue])];
        }
        _mapView.contentMode = UIViewContentModeScaleAspectFit;
        
        NSString *localImagePath = [CommonService addLocalRootPath:parkEntity.map_image ParkId:parkEntity.ID];
        DebugLog(@"%@", localImagePath);
        UIImage* img = [UIImage imageWithContentsOfFile:localImagePath];
        _mapView.image = img;
        //_mapView.imageURL = [NSURL URLWithString:parkEntity.map_image];
        _mapView.userInteractionEnabled = YES;
        [UIImageView lk_setImageDownloadDelegate:self];
        [self addSubview:_mapView];
        [_mapView addSubview:mapRouteView];
        
        //双击
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTap:)];
        [doubleTapGesture setNumberOfTapsRequired:2];
        [_mapView addGestureRecognizer:doubleTapGesture];
        
        mScale = self.frame.size.width/_mapView.frame.size.width;
        [self setMinimumZoomScale:mScale];
        [self setZoomScale:mScale];
        
        mutableArrayHotButton = [[NSMutableArray alloc] init];
        
        mutableArraySpot = array;
        
        for (int i = 0; i < [mutableArraySpot count]; i++) {
            SpotEntity *entity = [mutableArraySpot objectAtIndex:i];
            CGPoint point = {[entity.x doubleValue], [entity.y doubleValue]};
            [self addHotPot:point Entity:entity ID:i Scale:mScale];
        }
        
        lastScale = 0.0f;
    }
    
    return self;
}

#pragma mark - Zoom methods
-(void)handleDoubleTap:(UIGestureRecognizer*)gesture
{
    CGFloat newScale = self.zoomScale*1.5;
    CGRect zoonRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
    [self zoomToRect:zoonRect animated:YES];
    
    [self reSetHotButtonSize:newScale];
}

-(CGRect)zoomRectForScale:(CGFloat)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.frame.size.height / scale;
    zoomRect.size.width = self.frame.size.width /scale;
    zoomRect.origin.x = center.x -(zoomRect.size.width/2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height/2.0);
    return zoomRect;
}

#pragma mark - UIScrollViewDelegate
-(UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    DebugLog(@"%f,%f,%f, %f",_mapView.frame.origin.x, _mapView.frame.origin.y, _mapView.frame.size.width, _mapView.frame.size.height);
    return _mapView;
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    [scrollView setZoomScale:scale animated:NO];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self reSetHotButtonSize:scale];
    });
}

#pragma mark -UITouch
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    CGPoint touchPoint  = [[touches anyObject] locationInView:_mapView];
    NSValue *touchValue = [NSValue valueWithCGPoint:touchPoint];
//    [self performSelector:@selector(performTouchTestArea:)
//               withObject:touchValue
//               afterDelay:0.1];
    
}

- (UIButton *)addHotPot:(CGPoint )aTouchPoint
                 Entity:(SpotEntity *)entity
                     ID:(int)numberID
                  Scale:(CGFloat)scale {
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(aTouchPoint.x-11/scale, aTouchPoint.y-30/scale, 22/scale, 30/scale)];
    btn.layer.anchorPoint = CGPointMake(0.5, 1);
    if ([entity.ptype isEqualToString:@"1"] == YES) {
        [btn setBackgroundImage:[UIImage imageNamed:@"地图图标_景点.png"] forState:UIControlStateNormal];
    } else if ([entity.ptype isEqualToString:@"2"] == YES) {
        [btn setBackgroundImage:[UIImage imageNamed:@"地图图标_商业.png"] forState:UIControlStateNormal];
    } else if ([entity.ptype isEqualToString:@"3"] == YES) {
        [btn setBackgroundImage:[UIImage imageNamed:@"地图图标_厕所.png"] forState:UIControlStateNormal];
    }
    
    [btn addTarget:self action:@selector(btnSpotAction:)  forControlEvents:UIControlEventTouchUpInside];
    btn.tag = numberID;
    [self.mapView addSubview:btn];
    [self bringSubviewToFront:btn];
    
    if ([entity.ptype isEqualToString:@"1"] == YES) {
        btn.hidden = NO;
    } else {
        btn.hidden = YES;
    }
    
    [mutableArrayHotButton addObject:btn];
    [_mapView bringSubviewToFront:btn];
    
    return  btn;
}

- (void) showHotPointByType:(NSString *)type {
    for (int i = 0; i < [mutableArrayHotButton count]; i++) {
        UIButton *btn = [mutableArrayHotButton objectAtIndex:i];
        SpotEntity *entity = [mutableArraySpot objectAtIndex:i];
        
        if ([entity.ptype isEqualToString:type] == YES) {
            btn.hidden = NO;
        } else {
            btn.hidden = YES;
        }
    }
}

-(void)lk_finishDownLoad:(UIImage*)image {

}

- (void) btnSpotAction:(id)action
{
    DebugLog(@"ABC");
    UIButton * button = (UIButton *)action;
    SpotEntity *entity = [mutableArraySpot objectAtIndex:button.tag];
    
    if ([entity.ptype isEqualToString:@"1"] == YES) {
        
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:entity.title contentText:entity.content leftButtonTitle:@"取消" rightButtonTitle:@"确定"];
        [alert show];
        alert.leftBlock = ^() {
            NSLog(@"left button clicked");
        };
        alert.rightBlock = ^() {
            NSLog(@"right button clicked");
            [mapImageViewDelegate pushAction:entity ArraySpot:mutableArraySpot Index:button.tag];
        };
        alert.dismissBlock = ^() {
            NSLog(@"Do something interesting after dismiss block");
        };
    } else {
        NSString *title = @"厕所";
        if ([entity.ptype isEqualToString:@"2"] == YES) {
            title = @"商业";
        }
        DXAlertView *alert = [[DXAlertView alloc] initWithTitle:entity.title contentText:entity.content leftButtonTitle:@"确定" rightButtonTitle:nil];
        [alert show];
        alert.leftBlock = ^() {
            NSLog(@"left button clicked");
        };
        alert.rightBlock = ^() {
            NSLog(@"right button clicked");
            [mapImageViewDelegate pushAction:entity ArraySpot:mutableArraySpot Index:button.tag];
        };
        alert.dismissBlock = ^() {
            NSLog(@"Do something interesting after dismiss block");
        };
    }
}

- (void) reSetHotButtonSize:(CGFloat)scale {
    
    for (int i = 0; i < [mutableArrayHotButton count]; i++) {
        UIButton *btn = [mutableArrayHotButton objectAtIndex:i];
        
        btn.layer.anchorPoint = CGPointMake(0.5, 1);
        btn.bounds = CGRectMake((btn.frame.origin.x +11*scale -11) , (btn.frame.origin.y + 30*scale -30) , 22/scale, 30/scale);
        
        DebugLog(@"%f,%f,%f,%f,%f,%f", scale, mScale, btn.frame.origin.x - 11/scale, btn.frame.origin.y-30/scale, 22/scale, 30/scale);
    }
    
    _lastWidth = _mapView.frame.size.width;
    _lastHeight = _mapView.frame.size.height;
}

- (void)addRouteMap: (UIImage *)image {
    mapRouteView.image = image;
}

//- (void)performTouchTestArea:(NSValue *)inTouchPoint
//{
//    CGPoint aTouchPoint = [inTouchPoint CGPointValue];
//    [SKIndoorMapTransfer initWithCoordinate:@"333,1547,334,1653,682,1651,679,1540,576,1545" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Apple Store" delegate:self];
//    [SKIndoorMapTransfer initWithCoordinate:@"173,451,173,559,299,563,300,590,316,592,321,628,687,632,689,334,581,335,580,447,536,448,535,465,333,464,330,448" InPoint:aTouchPoint Inview:_mapView WithTitle:@"GAP" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"922,445,921,629,1013,628,1051,598,1049,448" InPoint:aTouchPoint Inview:_mapView WithTitle:@"优视一品" delegate:self];
//    [SKIndoorMapTransfer initWithCoordinate:@"1081,444,1078,598,1177,597,1215,532,1276,485,1275,444" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Samanth a Thavasa" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1289,444,1290,484,1371,466,1451,485,1455,444,1369,421" InPoint:aTouchPoint Inview:_mapView WithTitle:@"SWATCH" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1466,446,1467,485,1513,525,1562,595,1822,593,1818,573,1855,573,1854,443" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Izzue" delegate:self];
//
//    [SKIndoorMapTransfer initWithCoordinate:@"1823,632,1823,751,1866,746,1867,630" InPoint:aTouchPoint Inview:_mapView WithTitle:@"西城旅游局" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"231,810,229,917,420,915,420,801" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Fred Perry" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"420,915,420,801,580,795,580,917" InPoint:aTouchPoint Inview:_mapView WithTitle:@"CAMPER" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"64,1051,61,1652,333,1648,331,1421,451,1420,451,1500,577,1500,521,1437,498,1375,489,1292,497,1219,526,1147,580,1078,578,1048" InPoint:aTouchPoint Inview:_mapView WithTitle:@"ZARA" delegate:self];
//    
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"680,792,813,786,813,975,680,973" InPoint:aTouchPoint Inview:_mapView WithTitle:@"MAX&CO." delegate:self];
//    [SKIndoorMapTransfer initWithCoordinate:@"917,981,680,973,679,1076,749,1173,916,1173" InPoint:aTouchPoint Inview:_mapView WithTitle:@"LOVE MOSCHINO" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"917,981,916,1173,995,1126,1052,1126,1050,982" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Tissot" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"815,786,815,919,919,923,918,783" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Minnetonk" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"918,783,919,877,1036,776,1037,876" InPoint:aTouchPoint Inview:_mapView WithTitle:@"TENDENCE" delegate:self];
//    [SKIndoorMapTransfer initWithCoordinate:@"1036,776,1179,769,1210,819,1202,1102,1185,1130,1052,1127,1037,876" InPoint:aTouchPoint Inview:_mapView WithTitle:@"SEPHORA" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1293,808,1438,808,1437,849,1294,850" InPoint:aTouchPoint Inview:_mapView WithTitle:@"手表维修 " delegate:self];
//  
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1554,799,1585,779,1807,780,1806,932,1559,933" InPoint:aTouchPoint Inview:_mapView WithTitle:@"SWAROVSKI" delegate:self];
//    
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1559,933,1806,932,1805,1047,1560,1043" InPoint:aTouchPoint Inview:_mapView WithTitle:@"Juicy Couture" delegate:self];
//    
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1560,1043,1805,1047,1804,1163,1559,1160" InPoint:aTouchPoint Inview:_mapView WithTitle:@"DKNY" delegate:self];
//    [SKIndoorMapTransfer initWithCoordinate:@"1574,1199,1572,1406,1454,1412,1458,1582,1540,1590,1544,1701,1964,1705,1955,1287,1950,1197" InPoint:aTouchPoint Inview:_mapView WithTitle:@"H&M" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"1068,1308,1066,1430,932,1426" InPoint:aTouchPoint Inview:_mapView WithTitle:@"JurLique" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"764,1285,906,1284,906,1425,922,1429,924,1544,795,1544,795,1479,728,1474,726,1444" InPoint:aTouchPoint Inview:_mapView WithTitle:@"GUESS" delegate:self];
//    
//    [SKIndoorMapTransfer initWithCoordinate:@"229,917,580,914,579,1046,232,1047" InPoint:aTouchPoint Inview:_mapView WithTitle:@"CK" delegate:self];
//
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
